package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealSevriceImpl implements SetmealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
     @Autowired
     private DishMapper dishMapper;

    /**
     * 插入套餐
     * @param setmealDTO
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增一种套餐
        setmealMapper.save(setmeal);

         //获取套餐主键id
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });

   //    setmealDishMapper.getSetmealIdVByDishId();
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页
        //当前页码和数据
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用mapper接口
       Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        return new PageResult(total,result);
    }
    /**
     *根据id查询套餐和套餐中的菜品
     * @param id
     * @return
     */
    public SetmealVO getByIdWithDish(Long id) {
        //根据id查询套餐
     Setmeal setmeal=setmealMapper.getBydId(id);

     //根据id查询菜品
      List<SetmealDish> setmealDishes=setmealDishMapper.getDishsById(id);
      //将查到的套餐和套餐中的菜品川汇区
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealVO
     */
    public void update(SetmealVO setmealVO) {
        //修改套餐
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);


        //先删除再添加菜品
        setmealDishMapper.delete(setmealVO.getId());
        //修改套餐中包含的菜品
        //也就是插入菜品
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        if (setmealDishes.size()>0&&setmealDishes!=null){
               setmealDishes.forEach(setmealDish ->{
                   setmealDish.setSetmealId(setmealVO.getId());
               });
        }

        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 修改套餐起售、停售
     * @param status
     * @param id
     */
    public void startOrstop(Integer status, Long id) {
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);

        if (status== StatusConstant.DISABLE){
            //如果是停售状态，需要将套餐包含的菜品也要停售
            //1.将套餐id形成一个集合
            List<Long> setmealId=new ArrayList<>();
            setmealId.add(id);

           //select dish_id from setmeal_dish where setmeal_id in()
         List<Long> dishIds= setmealDishMapper.getDishIdBySetmealId(setmealId);
              if (dishIds.size()>0&&dishIds!=null){
                  //有菜品才停售
                  for (Long dishId : dishIds) {
                      Dish dish=new Dish();
                      dish.setId(dishId);
                      dish.setStatus(status);
                      dishMapper.update(dish);
                  }
              }
        }


    }

    /**
     * 套餐批量删除
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {

        for (Long id : ids) {
            //根据id查询到套餐
            Setmeal setmeal = setmealMapper.getBydId(id);
            //判断套餐是否是起售状态 如果是起售状态就不能删除
            if (setmeal.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            //直接删除套餐
            setmealMapper.delete(id);
            setmealDishMapper.delete(id);
        }



    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
